<template>
  <div class="home">
    <!-- 条件筛选查询操作 -->
    <el-form ref="form" :model="CarInfo" label-width="80px" size="mini">
      <el-row>
        <el-col :span="12">
          <el-form-item label="用户名">
            <el-input v-model="CarInfo.carName"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="地址">
            <el-input v-model="CarInfo.pinpai"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item size="large">
        <el-button type="primary" @click="selectInfo()">查询</el-button>
        <el-button type="info" @click="resetInfo()">重置</el-button>
        <el-button type="success" @click="addCarInfo()">新增</el-button>
      </el-form-item>
    </el-form>
    <!--显示列表数据 -->
    <el-table
        ref="multipleTable"
        :data="carList"
        tooltip-effect="dark"
        style="width: 100%"
        @selection-change="handleSelectionChange">
      <el-table-column
          type="selection"
          width="55">
      </el-table-column>
      <el-table-column
          label="#"
          type="index"
          width="50">
      </el-table-column>
      <el-table-column
          prop="carname"
          label="车辆名字"
          width="120">
      </el-table-column>
      <el-table-column
          prop="pinpai"
          label="品牌"
          width="120">
      </el-table-column>
      <el-table-column
          prop="type"
          label="类型"
          show-overflow-tooltip>
      </el-table-column>
      <el-table-column
          prop="pailiang"
          label="排量"
          width="180">
      </el-table-column>
      <el-table-column
          prop="carImg"
          label="图片"
          width="180">
        <template #default="scope">
          <img :src="scope.row.carImg"  min-width="70" height="70" />
        </template>
      </el-table-column>
      <el-table-column
          fixed="right"
          label="操作"
          width="200">
        <template #default="scope">
          <el-button @click="editCarInfo(scope.row)" type="primary" icon="el-icon-edit" circle size="small">编辑
          </el-button>
          <el-button @click="deleteCarInfo(scope.row.id)" type="danger" icon="el-icon-delete" circle size="small">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[2, 3, 5, 10]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
    </el-pagination>

    <!-- 新增或编辑的弹出框 -->
    <el-dialog title="用户信息" v-model="dialogFormVisible">
      <el-form :model="EditCarBean">
        <el-form-item label="车辆名" :label-width="formLabelWidth">
          <el-input v-model="EditCarBean.carName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="品牌" :label-width="formLabelWidth">
          <el-input type="text" v-model="EditCarBean.pinpai" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="类型" :label-width="formLabelWidth">
          <el-input v-model="EditCarBean.type" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="排量" :label-width="formLabelWidth">
          <el-input v-model="EditCarBean.pailiang" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="照片" :label-width="formLabelWidth">
          <el-upload
              action="#"
              list-type="picture-card"
              :limit="1"
              :show-file-list="showFileList"
              :auto-upload="true"
              :before-upload="beforeUploadForm"
              :http-request="imageChange">
            <img v-show="imgShow" :src="EditCarBean.carImg" style="width: 146px;height: 146px">
            <div v-show="!imgShow">上传文件</div>
          </el-upload>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addOrUpdateUser()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// @ is an alias to /src


import request from "@/config/request";

export default {
  name: 'Car',
  data() {
    return {
      // 分页相关
      // 数据条数
      total: 0,
      // 当前页
      currentPage: 1,
      pageSize: 5,
      showFileList:false,

      CarInfo: {
        carname: "",
        pinpai: ""
      },
      carList: [],
      dialogFormVisible: false,
      // 新增或编辑的数据对象
      EditCarBean: {
        // id判断是新增还是修改
        id: null,
        carName: "",
        pinpai: "",
        type: "",
        pailiang: "",
        carImg:""
      },
      imgShow:false,
      formLabelWidth: '80px'
    }
  },
  components: {},
  methods: {
    //图片上传
    // 开始上传前验证
    beforeUploadForm: function (file) {
      // 验证文件类型
      var testmsg = file.name.substring(file.name.lastIndexOf('.') + 1)
      var extension = testmsg === 'jpg' || testmsg === 'png' || testmsg === 'gif'
      if (!extension) {
        this.$message({
          message: '上传文件只能是jpg/png/gif格式!',
          duration: 1000,
          showClose: true,
          type: 'warning'
        })
      }
      return extension
    },
    // 提交图片
    imageChange: function (param, type) {
      // that.showFileList=true
      var that=this
      var formData = new FormData()
      formData.append('image', param.file);
      request.post('/api/upload', formData).then(function (res) {
        console.log(res);
        that.showFileList=true
        that.imgShow=false
        that.EditCarBean.carImg=res;



      });
    },
    // 分页相关
    handleSizeChange: function (currentSize) {
      this.pageSize = currentSize
      // 当页面条数发生变化的时候触发
      console.log("handleSizeChange::::" + currentSize)
      this.selectInfo(this.currentPage, currentSize)

    },
    handleCurrentChange: function (currentIndex) {
      // 当前页面（切换当前页面）发生变化时触发
      console.log("handleCurrentChange::::" + currentIndex)
      this.selectInfo(currentIndex, this.pageSize)

    }

    ,
    selectInfo: function (currentIndex, currentPageSize) {
      // 点击查询按钮
      console.log("发送请求")
      var that = this
      // 处理参数
      // var param=Qs.stringify(this.UserInfo)
      // console.log(param)
      // console.log(Qs.stringify(param))
      // 页面加载时，若没有传递参数，则使用默认分页参数
      if (!currentIndex) {
        currentIndex = this.currentPage
      }
      console.log("currentIndex" + currentIndex)
      if (!currentPageSize) {
        currentPageSize = this.pageSize
      }


      // var  param="&pageNum="+currentIndex+"&pageSize="+currentPageSize
      //   console.log(param)
      // 发送请求,
      // 1、查询操作可以使用get请求来进行操作？直接使用params传参即可
      //2、发送post请求时：可以进行参数处理,
      //       (1、引入qs对参数进行序列化处理 2、可以json方式传、但是需要在后台接收参数时进行简单处理)
      request.get('/api/car/getallcars', {
            params: {
              // userInfo: this.UserInfo,
              pageNum: currentIndex,
              pageSize: currentPageSize,
              username: this.CarInfo.username,
              useraddress: this.CarInfo.useraddress
            }
          }
      )
          .then(function (response) {
            console.log(response);
            // // 将响应回来的数据赋值给表格中的数据对象
            that.carList = response.list
            that.total = response.total
            that.currentPage = response.pageNum
            that.pageSize = response.pageSize
            that.CarInfo.carname = ""
          })
          .catch(function (error) {
            console.log(error);
          });


    },
    resetInfo: function () {
      // 点击重置按钮的
    },
    addCarInfo: function () {
      this.EditCarBean.carImg=""
      this.imgShow=false
      this.showFileList=false
      // 点击新增按钮
      // 弹出dialog框
      this.dialogFormVisible = true
      this.EditCarBean ={
        // id判断是新增还是修改
        id: null,
        carName: "",
        pinpai: "",
        type: "",
        pailiang: "",
        carImg:""
      }

    },
    handleSelectionChange: function () {

    },
    editCarInfo: function (rowData) {
      this.imgShow=true
      // 点击编辑操作的
      // 弹出dialog框
      this.dialogFormVisible = true
      console.log(rowData)
      // 编辑操作的模态框中的数据进行赋值操作
      // this.EditUserInfoBean=rowData
      this.EditCarBean = Object.assign({}, rowData)

    },
    // 新增或编辑的保存方法
    addOrUpdateUser: function () {
      var that = this
      // console.log(param)
      // console.log(Qs.stringify(param))
      // 新增或删除方法请求发送
      request.post('/api/car/addorupdatecar', this.EditCarBean)
          .then(function (response) {


            // console.log(response);
            // 响应数据判断，为ok；则关闭dialog框；刷新数据
            if (response == "ok") {
              that.dialogFormVisible = false
              that.selectInfo()
            }


          })
          .catch(function (error) {
            console.log(error);
          });
    },
    deleteCarInfo: function (deleteId) {
      console.log(deleteId)
      var that = this
      this.$confirm('此操作将永久删除该条记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 发送删除请求
        request.post('/api/car/deletecar', {"id": deleteId})
            .then(function (response) {
              console.log(response);
              // 删除成功后刷新页面
              that.selectInfo()
            })
            .catch(function (error) {
              console.log(error);
            });
        // 删除成功提示
        this.$message({
          type: 'success',
          message: '删除成功!'
        });
        // 删除失败进行提示
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    }

  }
  ,
  mounted: function () {
    this.selectInfo()
  }


  ,

}
</script>
